const myDb = require('../../../libs/mydb');
const { md5 } = require('../../../utiles/mycrypto')

module.exports = {

    //个人中心的渲染 传对应的用户ID值 回传昵称，性别，邮箱，头像
    personal_show: async function (req, res) {
        let { uid } = req.query;
        await myDb.singleSearch('ac_user_info', 'u_nick,usex,uemail,u_headers', `WHERE uid = ${uid}`)
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: 'success',
                        personalData: result
                    })
                }
            )
    },

    //修改个人中心信息 传对应的用户ID值 | 昵称，性别，邮箱，头像
    personal_modify: async function (req, res) {
        let data = req.body;
        console.log(data);
        for (let x in data) {
            if (data[x] == '') {
                res.send({
                    code: 400,
                    msg: 'Data cannot be empty'
                });
            }
        }
        await myDb.modify('ac_user_info',data, ` WHERE ac_user_info.uid = ${data.uid}`)
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: 'success'
                    })
                }
            )
    },

    //渲染自己发布的文章  传对应的用户ID值 回传文章ID 发布时间 文章内容
    personalarticle_show: async function (req, res) {
        let { uid } = req.query;
        await myDb.singleSearch('ac_hot_texts', 'tid,release_time,content_text', `WHERE uid = ${uid}`)
            .then(
                result => {
                    if (result) {
                        console.log(result)
                        res.send({
                            code: 200,
                            msg: 'success',
                            personalarticleData: result
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'no article',
                        })
                    }
                }
            )
    },

    //删除自己发布的文章 传对应的文章ID 
    personalarticle_del: async function (req, res) {
        await myDb.singleSearch('ac_comment', 'com_id', ` WHERE text_id=${req.body.tid}`)
            .then(
                result => {
                    if (result.length == 0) {
                        myDb.del('ac_hot_texts', ` WHERE tid=${req.body.tid}`)
                            .then(
                                result => {
                                    if (result) {
                                        res.send({
                                            code: 200,
                                            msg: 'OK'
                                        })
                                    } else {
                                        res.send({
                                            code: 400,
                                            msg: "Delete is NOt SUCCESS"
                                        })
                                    }
                                }
                            )

                    } else {
                        myDb.moreDel('ac_hot_texts,ac_comment',
                            ` WHERE ac_hot_texts.tid=${req.body.tid} AND ac_comment.text_id=${req.body.tid}`)
                            .then(
                                result => {
                                    if (result) {
                                        res.send({
                                            code: 200,
                                            msg: 'OK'
                                        })
                                    } else {
                                        res.send({
                                            code: 400,
                                            msg: "Delete is NOt SUCCESS"
                                        })
                                    }
                                }
                            )


                    }
                }
            )
    },

    //修改个人密码
    personalpassword_modify: async function (req, res) {
        let data = req.body;
        let dataA = {
            "u_password": md5(String(data.password))
        }
        await myDb.modify('ac_user', dataA, ` WHERE u_id=${data.uid}`)
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: "Modified successfully"
                    })
                }
            )
    }

}